package com.kingdee.jdy.util;

import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Value;


public class AccessTokenUtil {

/*    @Value("${demo.client_id:205022}")
    public static String client_id;

    @Value("${demo.client_secret:9f66202de5de5495f4df5ccd948648a7}")
    public static String client_secret;

    @Value("${demo.username:17220202021}")
    public static String username;

    @Value("${demo.password:kingdee0268}")
    public static String password;*/
    public static final String client_id = "205022";
    public static final String client_secret = "9f66202de5de5495f4df5ccd948648a7";
    public static final String username = "17220202021";
    public static final String password = "kingdee0268";

    public static String GetAccessToken() throws Exception {
        HashMap<String, Object> urlParam = new HashMap<>();
        urlParam.put("client_id", client_id);
        urlParam.put("client_secret", client_secret);
        urlParam.put("username", username);
        urlParam.put("password", password);

        JSONObject res = HttpUtil.doGet("https://api.kingdee.com/auth/user/access_token", urlParam, null);

        if (res != null) {
            JSONObject data = res.getJSONObject("data");
            if (data != null) {
                return data.getString("access_token");
            } else{
                throw new IOException("获取access_token失败！");
            }
        } else {
            throw new IOException("获取access_token失败！");
        }
    }

    public static JSONObject GetAccessTokenByAuth2(String code) {
        HashMap<String, Object> urlParam = new HashMap<>();
        urlParam.put("code", code);
        urlParam.put("client_id", client_id);
        urlParam.put("client_secret", client_secret);
        urlParam.put("grant_type", "authorization_code");
        urlParam.put("redirect_uri", "http://localhost:8080/helloworld");
        JSONObject res = null;
        try {
            res = HttpUtil.doPost("https://api.kingdee.com/auth/oauth2/access_token", urlParam, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    /**
     * 获取授权码
     * @param code
     * @return
     */
    public static JSONObject GetAuth_code(String access_token) {
        HashMap<String, Object> urlParam = new HashMap<>();
        urlParam.put("access_token", access_token);
        urlParam.put("target_id", client_id);
        JSONObject res = null;
        try {
            res = HttpUtil.doPost("https://api.kingdee.com/auth/user/auth_code", urlParam, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }


    /**
     * 打开本地浏览器
     * @throws IOException
     */
    public static void OpenLocalBrowse() throws IOException {
        //判断当前系统是否支持Java AWT Desktop扩展
        if(java.awt.Desktop.isDesktopSupported()){
            try{
                //创建一个URI实例,注意不是URL
                java.net.URI uri=java.net.URI.create("https://api.kingdee.com/auth/oauth2/authorize?client_id=205022&response_type=code&redirect_uri=http://localhost:8080/helloworld");
                //获取当前系统桌面扩展
                java.awt.Desktop dp=java.awt.Desktop.getDesktop();
                //判断系统桌面是否支持要执行的功能
                if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
                    //获取系统默认浏览器打开链接
                    dp.browse(uri);
                }
            }catch(java.lang.NullPointerException e){
                //此为uri为空时抛出异常
            }catch(java.io.IOException e){
                throw new IOException("无法打开本地浏览器！");
            }
        }

    }

    //GetAccessTokenFromAuth2


}
